CS = (typeof CS != "undefined")?  CS : {};

CS.Loaders = (typeof CS.Loaders != "undefined")?  CS.Loaders : {};

CS.Loaders.genmesh = function(loader, factory, params) {

  // then we have position, normal, texcoord float_arrays to pull out
  var farrays = [['position', true], ['normal', true], ['texture coordinate 0', false]];
  for (var i = 0; i < farrays.length; ++i) {
    var fname = farrays[i][0];

    var fnode = params.GetNode('//c:renderbuffer[@name="' + fname + '"]');
    if (!fnode) {
      if (farrays[i][1]) {
        CS.Log.error("Missing " + fname + " float_array in mesh node");
        return false;
      } else {
        CS.Log.warn("Missing " + fname + " float_array in mesh node"); 
        continue;
      }
    }
    
    if (fname == 'texture coordinate 0')
      fname = 'texcoord';

    factory.renderbuffers[fname] = loader.ParseRenderBuffer(fnode, fname);
  }
  
  //Grab the material if any.
  var material = params.GetNode('//c:material');
  if (material)
    factory.material = loader.Engine.materials[material.GetText()];

  // Load the submeshes.
  var inode = params.GetNode('//c:submesh/c:indexbuffer');
  if (!inode) {
    alert("Missing triangles/p in mesh node");
    return false;
  }
  
  var snodes = params.GetNodes('//c:submesh');  
  for (var j = 0; j < snodes.length; j++)  
  { 
    var submeshNode = snodes[j];
    var sub = new CS.MeshFactoryWrapper.Submesh(factory);
    //Grab the material if any.
    var material = submeshNode.GetNode('//c:material');
    if (material)
      sub.material = loader.Engine.materials[material.GetText()];
    
    //indexbuffer
    var fnode = submeshNode.GetNode('//c:indexbuffer');
    sub.renderbuffers['indexbuffer'] = loader.ParseRenderBuffer(fnode, 'indexbuffer');
    
    //shadervars
    var nodes = submeshNode.GetNodes('//c:shadervar'); 
    for (var i = 0; i < nodes.length; i++)   
    { 
      var svNode = nodes[i];
      var name = svNode.GetAttribute('name');
      var type = svNode.GetAttribute('type');
      var val = svNode.GetText(); 
      if (type == 'texture')
        sub.shadervars[name] = {type: type, value: loader.Engine.textures[val]};
      else
        sub.shadervars[name] = {type: type, value: val};
      CS.Log.warn("GENMESH: shadervar: "+name+', '+type+', '+val);  
    }   
  }

  // figure out the bounding box
  var minx = Infinity, miny = Infinity, minz = Infinity;
  var maxx = -Infinity, maxy = -Infinity, maxz = -Infinity;
  var npoints = Math.floor(factory.renderbuffers.position.data.length / 3);
  for (var i = 0; i < npoints; ++i) {
    var x = factory.renderbuffers.position.data[i*3  ];
    var y = factory.renderbuffers.position.data[i*3+1];
    var z = factory.renderbuffers.position.data[i*3+2];
    
    minx = Math.min(minx, x);
    miny = Math.min(miny, y);
    minz = Math.min(minz, z);

    maxx = Math.max(maxx, x);
    maxy = Math.max(maxy, y);
    maxz = Math.max(maxz, z);
  }

  factory.bbox = {
    min: { x: minx, y: miny, z: minz },
    max: { x: maxx, y: maxy, z: maxz }
  };
  
};
